﻿Namespace Core.Controls
    Partial Public Class ImageControl

        Public Enum imageKindEnum
            Local
            Twitter
            Stream
        End Enum

        Private _imageK As imageKindEnum
        Private _Base As ImageObject
        Private _selected As Boolean


        Public Property Base() As ImageObject
            Get
                Return _Base
            End Get
            Set(ByVal value As ImageObject)
                _Base = value
            End Set
        End Property

        Public Property ImageKind() As imageKindEnum
            Get
                Return _imageK
            End Get
            Set(ByVal value As imageKindEnum)
                _imageK = value
            End Set
        End Property

        Public Property Image() As ImageSource
            Get
                Return Image1.Source
            End Get
            Set(ByVal value As ImageSource)
                Image1.Source = value
            End Set
        End Property

        Public Property Selected As Boolean
            Get
                Return CheckBox1.IsChecked
            End Get
            Set(ByVal value As Boolean)
                CheckBox1.IsChecked = value
            End Set
        End Property

        Private Function ConvertImage(ByVal src As String, _
                                  ByVal PixelHeight As Integer, _
                                  ByVal PixelWidth As Integer) _
                                  As Windows.Media.Imaging.BitmapImage
            Dim bm1 As New Windows.Media.Imaging.BitmapImage
            bm1.BeginInit()
            Dim uri As New Uri(src)
            bm1.UriSource = uri
            If PixelHeight > 0 Then
                bm1.DecodePixelHeight = PixelHeight
            End If
            If PixelWidth > 0 Then
                bm1.DecodePixelHeight = PixelWidth
            End If
            bm1.EndInit()
            Return bm1

        End Function

        Private Function ConvertImage(ByVal src As IO.Stream, _
                                  ByVal PixelHeight As Integer, _
                                  ByVal PixelWidth As Integer) _
                                  As Windows.Media.Imaging.BitmapImage
            Dim bm1 As New Windows.Media.Imaging.BitmapImage
            bm1.BeginInit()
            bm1.StreamSource = src
            If PixelHeight > 0 Then
                bm1.DecodePixelHeight = PixelHeight
            End If
            If PixelWidth > 0 Then
                bm1.DecodePixelHeight = PixelWidth
            End If
            bm1.EndInit()
            Return bm1

        End Function

        Delegate Sub ImageUpdater_String(ByVal Source As Uri)
        Public Sub UpdateImage(ByVal UriSource As Uri)
            Dim dec As BitmapDecoder
            Dim bm As BitmapSource
            Try
                If UriSource.IsFile Then
                    dec = BitmapDecoder.Create(UriSource, BitmapCreateOptions.None, BitmapCacheOption.None)
                    If dec.Frames(0).Thumbnail Is Nothing Then
                        bm = ConvertImage(UriSource.LocalPath, 100, 100)
                    Else
                        bm = dec.Frames(0).Thumbnail
                    End If
                Else
                    dec = BitmapDecoder.Create(UriSource, BitmapCreateOptions.None, BitmapCacheOption.None)
                    bm = dec.Frames(0)
                End If
            Catch ex As Exception
                Console.WriteLine(String.Format("{0}({1})", ex.Message, UriSource.ToString))
            End Try

            Me.Image1.Source = bm

        End Sub

        Public Sub New(ByVal Image As ImageObject)
            InitializeComponent()
            Me.Base = Image
            Try
                Dispatcher.BeginInvoke(New ImageUpdater(AddressOf UpdateImage), _
                                       RenderPriority, _
                                       Image.OriginalUri)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try


        End Sub

        Delegate Sub ImageUpdater(ByVal Source As Uri)

        Public Sub New()

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

        End Sub

        Private Sub CheckBox1_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles CheckBox1.Checked
            CurPic = Me.Base.OriginalUri
        End Sub

        Private Sub ImageGrid_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            CurPic = Me.Base.OriginalUri
            If Me.Selected = True Then
                Me.Selected = False
            Else
                Me.Selected = True
            End If
        End Sub
    End Class
End Namespace

